package com.zhang.exception;


import com.zhang.domain.RespBean;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.FileNotFoundException;

/**
 * 全局异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler({ClassCastException.class,ArithmeticException.class})
    public String handleException1(Exception e){
        return "error1";//返回视图名
    }

    @ExceptionHandler(FileNotFoundException.class)
    public String  handleException2(Exception e){
        return "error2";
    }

    @ExceptionHandler(NullPointerException.class)
    public String  handleException3(Exception e){
        return "error3";
    }

    @ExceptionHandler(MyException.class)
    @ResponseBody
    public RespBean handleException4(Exception e){
        return RespBean.error("自定义异常");//直接响应数据给前端
    }

}











